﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Reborn_Server.App1AppHandler
{
    public class Message
    {
        public long CreatureID = -1;
        public string Text = "";
        public string Type = "";

        public string itemLinksXml = "<itemlinks/>";

        public Message() { }

        public string toXML()
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("message");
            xmldoc.WriteAttributeString("CreatureID", CreatureID.ToString());
            xmldoc.WriteAttributeString("Type", Type.ToString());
            
            xmldoc.WriteStartElement("Text");
            xmldoc.WriteRaw(Message.escapeXmlText(Text));
            xmldoc.WriteEndElement();

            xmldoc.WriteRaw(itemLinksXml);

            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();

            stringWriter.Flush();

            result = stringWriter.ToString();

            return result;
        }

        public static string escapeXmlText(string input)
        {
            input = input.Replace("&", "&amp;");
			while (input.IndexOf("<") > -1)	
			{ 
				input = input.Replace("<","&lt;"); 
			}
			while (input.IndexOf('"') > -1)	
			{ 
				input = input.Replace("\"","&quot;"); 
			}
			while (input.IndexOf("'") > -1)	
			{ 
				input = input.Replace("'","&apos;"); 
			}

            return input;
        }
    }
}
